﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OnlineLibrary.Controllers
{
    public class FormulaController : Controller
    {
        //
        // GET: /Formula/
        OnlineLibrary.Linq.OnlineLibraryDBDataContext fine = new OnlineLibrary.Linq.OnlineLibraryDBDataContext();
        public ActionResult Index()
        {
            if ((Boolean.Parse(Session["Admin"].ToString()) == true) || (Boolean.Parse(Session["Staff"].ToString()) == true))
            {
                IEnumerable<OnlineLibrary.Linq.tblFomula> list = fine.tblFomulas.ToList();
                return View(list);
            }
            else
            {
                return Redirect("Shared/Error");
            } 
        }

        //
        // GET: /Formula/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Formula/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Formula/Create

        [HttpPost]
        public ActionResult Create(OnlineLibrary.Linq.tblFomula collection)
        {
            try
            {
                // TODO: Add insert logic here
                if ((Boolean.Parse(Session["Admin"].ToString()) == true) || (Boolean.Parse(Session["Staff"].ToString()) == true))
                {
                    fine.tblFomulas.InsertOnSubmit(collection);
                    fine.SubmitChanges();
                }
                else
                {
                    return Redirect("Shared/Error");
                } 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Formula/Edit/5
 
        public ActionResult Edit(int id)
        {
            OnlineLibrary.Linq.tblFomula UserView = fine.tblFomulas.Single(p => p.FomulaId == id);
            return View(UserView);
        }

        //
        // POST: /Formula/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, OnlineLibrary.Linq.tblFomula collection)
        {
            try
            {
                // TODO: Add update logic here
                var list = fine.tblFomulas.SingleOrDefault(p => p.FomulaId == id);
                list.FomulaId = collection.FomulaId;
                list.FomulaName = collection.FomulaName;
                list.Fomula = collection.Fomula;
                if ((Boolean.Parse(Session["Admin"].ToString()) == true))
                {
                    fine.SubmitChanges();
                }
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Formula/Delete/5
 
        public ActionResult Delete(int id)
        {
            if ((Boolean.Parse(Session["Admin"].ToString()) == true) || (Boolean.Parse(Session["Staff"].ToString()) == true))
            {
                OnlineLibrary.Linq.tblFomula UserView = fine.tblFomulas.Single(p => p.FomulaId == id);
                return View(UserView);
            }
            else
            {
                return Redirect("Shared/Error");
            } 
        }

        //
        // POST: /Formula/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                if ((Boolean.Parse(Session["Admin"].ToString()) == true) || (Boolean.Parse(Session["Staff"].ToString()) == true))
                {
                    // TODO: Add delete logic here
                    var list = fine.tblFomulas.Single(p => p.FomulaId == id);
                    fine.tblFomulas.DeleteOnSubmit(list);
                    fine.SubmitChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    return Redirect("Shared/Error");
                } 
            }
            catch
            {
                return View();
            }
        }
    }
}
